package org.budo.redis.repository.spring.data.graph;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisConnectionUtils;

/**
 * @author lmw
 * @see org.springframework.data.redis.core.RedisTemplate#execute(org.springframework.data.redis.core.RedisCallback,
 *      boolean, boolean)
 */
public class BudoSpringDataRedisGraphServiceImpl implements BudoSpringDataRedisGraphService {
    public static BudoSpringDataRedisGraphService INSTANCE = null;

    public BudoSpringDataRedisGraphServiceImpl() {
        INSTANCE = this;
    }

    @Override
    public byte[] get(RedisConnectionFactory redisConnectionFactory, byte[] key) {
        RedisConnection connection = null;
        try {
            connection = RedisConnectionUtils.getConnection(redisConnectionFactory);
            return connection.get(key);
        } finally {
            RedisConnectionUtils.releaseConnection(connection, redisConnectionFactory);
        }
    }

    @Override
    public String set(RedisConnectionFactory redisConnectionFactory, byte[] key, byte[] value) {
        RedisConnection connection = null;
        try {
            connection = RedisConnectionUtils.getConnection(redisConnectionFactory);
            connection.set(key, value);
            return null;
        } finally {
            RedisConnectionUtils.releaseConnection(connection, redisConnectionFactory);
        }
    }

    @Override
    public Long del(RedisConnectionFactory redisConnectionFactory, byte[]... keys) {
        RedisConnection connection = null;
        try {
            connection = RedisConnectionUtils.getConnection(redisConnectionFactory);
            connection.del(keys);
            return null;
        } finally {
            RedisConnectionUtils.releaseConnection(connection, redisConnectionFactory);
        }
    }
}
